package trigger;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.DigestInputStream;
import java.security.MessageDigest;

public class Hash 
{
	
public static String file(File file)
{
	
	String output = new String();
	
try
{
InputStream is = new FileInputStream(file);	
MessageDigest digest = MessageDigest.getInstance("SHA-1");
DigestInputStream digin = new DigestInputStream(is,digest);
byte[] buffer = new byte[4096];
	try{
		while ( digin.read(buffer, 0, 4096) != - 1)  
		{  
			//Do nothing, the digest is calculated automatically as file is read in.      
		}
	byte[] md5sum = digest.digest();
	BigInteger bigInt = new BigInteger(1, md5sum);
	output = bigInt.toString(16);
	}
	catch(Exception e) 
	{
	throw new RuntimeException("Unable to process file for SHA-1", e);
	}
	finally
	{
		try
			{
			is.close();
			}
			catch(IOException e)
			{
			throw new RuntimeException("Unable to close input stream for SHA-1 calculation", e);
			}
	}		
}
catch(Exception ex){;}
	
	return output;
}

public static String string(String str)
{
	String output = new String();  
	try
	{  
		MessageDigest md = MessageDigest.getInstance("MD5"); 
		byte[] data = str.getBytes();
		byte[] md5sum = md.digest(data);
		BigInteger hash = new BigInteger(1, md5sum);
		output = hash.toString(16);
	}
	catch (Exception e)
	{  
	} 
             
return output; 
}

}

